<?php
namespace app\admin\controller;

use app\common\model\ZhMemberAlllog as ZhMemberAlllogModel;
use think\Db;
/**
 * 用户访问记录控制器
 * Class Zhmemberalllog
 * @package app\index\controller
 */
class Zhmemberalllog extends Base
{
    /**
     * [index 获取所有活动]
     * @param type 1浏览量 2独立用户 3独立IP
     * @return mixed|string
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhMemberAlllogModel;
        try{
            //总浏览量
            $todaystart = strtotime(date('Y-m-d'));
            $data['pv'] = $model->getAllCount('');
            //浏览增长百分比
            $todayalllog = $model->getAllCount(['create_time'=>['between',[$todaystart,$todaystart+24*60*60]]]);
            $yesterdayalllog = $model->getAllCount(['create_time'=>['between',[$todaystart-24*60*60,$todaystart]]]);
            if($yesterdayalllog == 0){
                if($todayalllog == 0){
                    $data['alllog_compare_yesterday'] = 0;
                }else{
                    $data['alllog_compare_yesterday'] = 100;
                }
            }else{
                $data['alllog_compare_yesterday'] = number_format(($todayalllog - $yesterdayalllog) / $yesterdayalllog * 100,2);
            }
            //独立用户
            $lognum = Db::query("select count(distinct mid) as num from app_zh_member_log");
            $data['uv'] = $lognum[0]['num'];
            //独立用户增长百分比
            $todayend = $todaystart+24*60*60;
            $yesterdaystart = $todaystart-24*60*60;
            $todaylog = Db::query("select count(distinct mid) as num from app_zh_member_log where create_time between '{$todaystart}' and '{$todayend}'");
            $yesterdaylog = Db::query("select count(distinct mid) as num from app_zh_member_log where create_time between '{$yesterdaystart}' and '{$todaystart}'");
            if($yesterdaylog[0]['num'] == 0){
                if($todaylog[0]['num'] == 0){
                    $data['log_compare_yesterday'] = 0;
                }else{
                    $data['log_compare_yesterday'] = 100;
                }
            }else{
                $data['log_compare_yesterday'] = number_format(($todaylog[0]['num'] - $yesterdaylog[0]['num']) / $yesterdaylog[0]['num'] * 100,2);
            }
            //独立IP
            $ipnum = Db::query("select count(distinct ip) as num from app_zh_member_alllog");
            $data['ip'] = $ipnum[0]['num'];
            //独立IP增长百分比
            $todayip = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$todaystart}' and '{$todayend}'");
            $yesterdayip = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$yesterdaystart}' and '{$todaystart}'");
            if($yesterdayip[0]['num'] == 0){
                if($todayip[0]['num'] == 0){
                    $data['ip_compare_yesterday'] = 0;
                }else{
                    $data['ip_compare_yesterday'] = 100;
                }
            }else{
                $data['ip_compare_yesterday'] = number_format(($todayip[0]['num'] - $yesterdayip[0]['num']) / $yesterdayip[0]['num'] * 100,2);
            }

            $seven = $todaystart - 7*24*60*60;
            $thirty = $todaystart - 30*24*60*60;
            //趋势图
            for($i=0;$i<12;$i++){
                //今天
                $start = $todaystart + 2*60*60*$i;
                $end = $todaystart + 2*60*60*($i+1);
                //昨天
                $yesterdaystart = $todaystart + 2*60*60*$i - 24*60*60;
                $yesterdayend = $todaystart + 2*60*60*($i+1) - 24*60*60;
                //七天前
                $sevenstart = $seven + 2*60*60*$i;
                $sevenend = $seven + 2*60*60*($i+1);
                //三十天前
                $thirtystart = $thirty + 2*60*60*$i;
                $thirtyend = $thirty + 2*60*60*($i+1);
                $data['data'][$i]['time'] =  date('H:i',$start).'--'.date('H:i',$end);
                $data['data'][$i]['pv'] = $model->getAllCount(['create_time'=>['between',[$start,$end]]]);
                $lognum1 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where create_time between '{$start}' and '{$end}'");
                $data['data'][$i]['uv'] = $lognum1[0]['num'];
                $alllognum1 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$start}' and '{$end}'");
                $data['data'][$i]['ip'] = $alllognum1[0]['num'];
                if($param['type'] == 1){//pv
                    $data['today_log'][] = $data['data'][$i]['pv'];
                    $data['yesterday_log'][] = $model->getAllCount(['create_time'=>['between',[$yesterdaystart,$yesterdayend]]]);
                    $data['seven_log'][] = $model->getAllCount(['create_time'=>['between',[$sevenstart,$sevenend]]]);
                    $data['thirty_log'][] = $model->getAllCount(['create_time'=>['between',[$thirtystart,$thirtyend]]]);
                }elseif ($param['type'] == 2){//uv
                    $data['today_log'][] = $lognum1[0]['num'];
                    $lognum2 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where create_time between '{$yesterdaystart}' and '{$yesterdayend}'");
                    $data['yesterday_log'][] = $lognum2[0]['num'];
                    $lognum3 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where create_time between '{$sevenstart}' and '{$sevenend}'");
                    $data['seven_log'][] = $lognum3[0]['num'];
                    $lognum4 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where create_time between '{$thirtystart}' and '{$thirtyend}'");
                    $data['thirty_log'][] = $lognum4[0]['num'];
                }elseif ($param['type'] == 3){
                    $data['today_log'][] = $alllognum1[0]['num'];
                    $alllognum2 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$yesterdaystart}' and '{$yesterdayend}'");
                    $data['yesterday_log'][] = $alllognum2[0]['num'];
                    $alllognum3 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$sevenstart}' and '{$sevenend}'");
                    $data['seven_log'][] = $alllognum3[0]['num'];
                    $alllognum4 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where create_time between '{$thirtystart}' and '{$thirtyend}'");
                    $data['thirty_log'][] = $alllognum4[0]['num'];
                }
            }
        }catch (\Exception $e) {
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
    /**
     * [operator 运营商统计]
     * @param operator 1移动 2联通 3电信 4其他
     * @param type 1浏览量 2独立用户 3独立IP
     * @return mixed|string
     * @author [C。]
     */
    public function operator()
    {
        $param = input('param.');
        $model = new ZhMemberAlllogModel;
        try{
            $starttime = strtotime(date('Y-m-d',time()-$param['day']*24*60*60));
            for($i=0;$i<$param['day'];$i++){
                $start = $starttime + ($i+1)*24*60*60;
                $end = $starttime+($i+2)*24*60*60;
                if($param['type'] == 1){
                    $todayalllog1 = $model->getAllCount(['create_time'=>['between',[$start,$end]],'operator'=>1]);
                    $logarr['logarr1'][] = $todayalllog1;
                    $todayalllog2 = $model->getAllCount(['create_time'=>['between',[$start,$end]],'operator'=>2]);
                    $logarr['logarr2'][] = $todayalllog2;
                    $todayalllog3 = $model->getAllCount(['create_time'=>['between',[$start,$end]],'operator'=>3]);
                    $logarr['logarr3'][] = $todayalllog3;
                    $todayalllog4 = $model->getAllCount(['create_time'=>['between',[$start,$end]],'operator'=>4]);
                    $logarr['logarr4'][] = $todayalllog4;
                }elseif($param['type'] == 2){
                    $lognum1 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where operator = '1' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr1'][] = $lognum1[0]['num'];
                    $lognum2 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where operator = '2' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr2'][] = $lognum2[0]['num'];
                    $lognum3 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where operator = '3' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr3'][] = $lognum3[0]['num'];
                    $lognum4 = Db::query("select count(distinct mid) as num from app_zh_member_alllog where operator = '4' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr4'][] = $lognum4[0]['num'];
                }elseif($param['type'] == 3){
                    $alllognum1 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where operator = '1' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr1'][] = $alllognum1[0]['num'];
                    $alllognum2 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where operator = '2' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr2'][] = $alllognum2[0]['num'];
                    $alllognum3 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where operator = '3' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr3'][] = $alllognum3[0]['num'];
                    $alllognum4 = Db::query("select count(distinct ip) as num from app_zh_member_alllog where operator = '4' and create_time between '{$start}' and '{$end}'");
                    $logarr['logarr4'][] = $alllognum4[0]['num'];
                }
            }
            $data['alllog'] = $logarr;
            $operator = Db::query('select operator as name,count(operator) as value from app_zh_member_alllog group by operator order by value desc');
            foreach($operator as &$v){
                $operat = $v['name'];
                $lognum = Db::query("select count(distinct mid) as num from app_zh_member_alllog where operator = '{$operat}'");
                $v['uv'] = $lognum[0]['num'];
                $operatornum = Db::query("select count(operator) as num from app_zh_member_alllog where operator = '{$operat}'");
                $v['pv'] = $operatornum[0]['num'];
                $alllognum = Db::query("select count(distinct ip) as num from app_zh_member_alllog where operator = '{$operat}'");
                $v['ip'] = $alllognum[0]['num'];
                if($v['name'] == 1){
                    $v['name'] = '移动';
                }elseif($v['name'] == 2){
                    $v['name'] = '联通';
                }elseif($v['name'] == 3){
                    $v['name'] = '电信';
                }else{
                    $v['name'] = '其他';
                }
            }
            $data['operator'] = $operator;
        }catch (\Exception $e) {
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
}